//
//  MoreInfoViewController.m
//  TestSchool
//
//  Created by 吴 晓泉 on 12-11-8.
//  Copyright (c) 2012年 yaoxia. All rights reserved.
//

#import "MoreInfoViewController.h"

#define kLeft 20.0f
#define kTop 20.0f
#define kWidth 280.0f
#define kLabelHeight 25.0f
#define kTextViewHeight 90.0f
#define kMargin 10.0f
#define kFontSize 16.0f

@interface MoreInfoViewController ()

@end

@implementation MoreInfoViewController

@synthesize questionId,scrollView,viewArray;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.viewArray = [NSMutableArray arrayWithCapacity:10];
    self.title = @"More";
}

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self getMoreData];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    self.questionId = nil;
    self.scrollView = nil;
    request = nil;
    self.viewArray = nil;
    
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

-(float)getTextHeight:(NSString *)text
{
//    CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:kFontSize] forWidth:kWidth lineBreakMode:UILineBreakModeWordWrap];
//    
//    return size.height;
    CGSize newSize = [text
                      
                      sizeWithFont:[UIFont systemFontOfSize:kFontSize]
                      
                      constrainedToSize:CGSizeMake(kWidth,9999)
                      
                      lineBreakMode:UILineBreakModeTailTruncation];
    return newSize.height;
}

-(void)setViewsCoordY
{
    float totalY = kTop;
    for (UIView *v in self.viewArray) {
        CGRect oldFrame = v.frame;
        CGSize oldSize = oldFrame.size;
        CGPoint oldCoord = oldFrame.origin;
        oldCoord.y = totalY;
        v.frame = CGRectMake(oldCoord.x, oldCoord.y, oldSize.width, oldSize.height);
        totalY = totalY + kMargin +oldSize.height;
    }
}

-(void)setScrollViewContentHeight
{
    UIView *v = [self.viewArray objectAtIndex:(self.viewArray.count - 1)];
    float scrollHeight = v.frame.origin.y +v.frame.size.height + kTop;
    self.scrollView.contentSize = CGSizeMake(320.0f, scrollHeight);
}

-(void)getMoreData
{
    [self showWaitView:nil];
    //NSString * URL_LOGIN = @"http://218.24.94.212/Test/login.jsp";
    AppDelegate *delegate=(AppDelegate*)[[UIApplication sharedApplication]delegate];
    NSString * URL_LOGIN = [NSString stringWithFormat:@"%@/Test/getOneMore.jsp",delegate.baseUrl];
    
    NSURL *url = [NSURL URLWithString:URL_LOGIN];
    request = [ASIFormDataRequest requestWithURL:url];
    
    [request setPostValue:self.questionId forKey:@"questionid"];
    
    
    [request setCompletionBlock:^{
        
        [self hideWaitView];
        
        // Use when fetching text data
        NSString *responseString = [request responseString];
        
        //NSLog(@"responseString = %@",responseString);//显示收到的信息
        
        id json = [responseString JSONValue];
        //NSLog(@"meg = %@",[json objectForKey:@"drugInfo"]);
        if ([[json objectForKey:@"code"]intValue] == 500) {
            UIAlertView * messageAlert = [[UIAlertView alloc]
                                          initWithTitle:@"Message"
                                          message:[NSString stringWithFormat:@"%@",[json objectForKey:@"message"]]
                                          delegate:self
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];
            [messageAlert show];
            return ;
        }
        [self dillWithData:json];
        
        // Use when fetching binary data
        //NSData *responseData = [request responseData];
    }];
    [request setFailedBlock:^{
        [self hideWaitView];
        NSError *error = [request error];
        
        NSLog(@"error = %@",error);
        UIAlertView *alert = [[UIAlertView alloc]
                              initWithTitle:@"Network error"
                              message:@"Please check your network"
                              delegate:self
                              cancelButtonTitle:@"OK"
                              otherButtonTitles:nil];
        [alert show];
    }];
    [request startAsynchronous];
}

-(void)dillWithData:(NSDictionary *)data
{
    
    //NSLog(@"处理网络数据:%@",data);
    [self.viewArray removeAllObjects];
    
    [self addViewsWithData:data title:@"chief complaint:" key:@"chiefcomplaint"];
    [self addViewsWithData:data title:@"history of present illness:" key:@"historyillness"];
    [self addViewsWithData:data title:@"family history:" key:@"familyhistory"];
    [self addViewsWithData:data title:@"social history:" key:@"socialhistory"];
    [self addViewsWithData:data title:@"physical exam:" key:@"physicalexam"];
    [self addViewsWithData:data title:@"Current Medications:" key:@"CurrentMedications"];
    [self addViewsWithData:data title:@"Lab Results:" key:@"LabResults"];
    
    AppDelegate *delegate=(AppDelegate*)[[UIApplication sharedApplication]delegate];
    NSString *imageStr1 = [data objectForKey:@"image1"];
    if (imageStr1.length >0 ) {
        UIWebView *webView = [[UIWebView alloc]initWithFrame:CGRectMake(kLeft, 0.0f, kWidth, kWidth)];
        NSURL *mUrl =[NSURL URLWithString:[NSString stringWithFormat:@"%@/%@",delegate.baseUrl,imageStr1]];
        NSURLRequest *request2 =[NSURLRequest requestWithURL:mUrl];
        [webView loadRequest:request2];
        [self.viewArray addObject:webView];
    }
    
    NSString *imageStr2 = [data objectForKey:@"image2"];
    if (imageStr2.length >0 ) {
        UIWebView *webView = [[UIWebView alloc]initWithFrame:CGRectMake(kLeft, 0.0f, kWidth, kWidth)];
        NSURL *mUrl =[NSURL URLWithString:[NSString stringWithFormat:@"%@/%@",delegate.baseUrl,imageStr2]];
        NSURLRequest *request2 =[NSURLRequest requestWithURL:mUrl];
        [webView loadRequest:request2];
        [self.viewArray addObject:webView];
    }
    
    //-------完成fill全部控件-----------
    [self setViewsCoordY];
    [self setScrollViewContentHeight];
    for (UIView *v in self.viewArray) {
        [self.scrollView addSubview:v];
    }
}

-(void)addViewsWithData:(NSDictionary *)data title:(NSString *)titleText key:(NSString *)keyWord
{
    UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(kLeft, 0.0f, kWidth, kLabelHeight)];
    label1.text = titleText;
    label1.backgroundColor = [UIColor lightGrayColor];
    NSString *content1 = [data objectForKey:keyWord];
    UITextView *textView1 = [[UITextView alloc]initWithFrame:CGRectMake(kLeft, 0.0f, kWidth,[self getTextHeight:content1]+20.0f)];
    textView1.text = content1;
    textView1.font = [UIFont systemFontOfSize:kFontSize];
//    float newHeight = textView1.contentSize.height;
//    textView1.frame = CGRectMake(kLeft, 0.0f, kWidth, newHeight+ 15.0f);
    
    textView1.editable = NO;
    [self addRoundBound:textView1];
    
    [self.viewArray addObject:label1];
    [self.viewArray addObject:textView1];
}

@end
